<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-back" @click="goBack">
        <image src="../../static/icon-right.png" class="back-icon"></image>
      </view>
      <view class="nav-title">附近加油</view>
      <view class="nav-right"></view>
    </view>
    
    <!-- 加油站图片 -->
    <view class="station-image-container">
      <image :src="stationData.imageUrl" class="station-image"></image>
    </view>
    
    <!-- 加油站信息 -->
    <view class="station-info">
      <view class="station-name">{{ stationData.name }}</view>
      <view class="station-meta">
        <view class="meta-item">
          <text class="meta-label">营业时间：</text>
          <text class="meta-value">{{ stationData.openTime }}</text>
        </view>
        <view class="meta-item">
          <text class="meta-label">地址：</text>
          <text class="meta-value">{{ stationData.address }}</text>
        </view>
      </view>
      
      <!-- 联系电话和导航按钮 -->
      <view class="action-buttons">
        <view class="call-btn" @click="makePhoneCall">
          <image src="../../static/icon-address.png" class="btn-icon"></image>
          <text class="btn-text">电话</text>
        </view>
        <view class="navigate-btn" @click="handleNavigate">
          <image src="../../static/icon-Myaddress.png" class="btn-icon"></image>
          <text class="btn-text">导航</text>
        </view>
      </view>
    </view>
    
    <!-- 油价详情 -->
    <view class="oil-price-section">
      <view class="section-title">油价详情</view>
      <view class="oil-price-list">
        <view class="oil-price-item" v-for="(price, type) in stationData.oilPrices" :key="type">
          <view class="oil-type">{{ type }}</view>
          <view class="oil-price">{{ price }}</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';

// 初始化加油站数据
const stationData = ref({
  name: '',
  address: '',
  openTime: '',
  phone: '',
  latitude: 0,
  longitude: 0,
  imageUrl: '',
  oilPrices: {
    '92#': '¥5.85/L',
    '95#': '¥6.25/L',
    '98#': '¥7.05/L',
    '0#': '¥5.55/L'
  }
});

onMounted(() => {
  // 获取事件通道
  const eventChannel = uni.getOpenerEventChannel();
  
  // 监听来自上一个页面的数据
  eventChannel.on('stationData', function(data) {
    console.log('收到加油站数据:', data);
    stationData.value = {
      ...stationData.value,
      ...data
    };
  });
});

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 拨打电话
const makePhoneCall = () => {
  const phoneNumber = stationData.value.phone || '400-123-4567'; // 使用默认虚拟电话
  uni.makePhoneCall({
    phoneNumber: phoneNumber,
    success: () => {
      console.log('拨打电话成功');
    },
    fail: (err) => {
      console.error('拨打电话失败:', err);
      uni.showToast({ title: '拨打电话失败', icon: 'none' });
    }
  });
};

// 处理导航
const handleNavigate = () => {
  if (!stationData.value.latitude || !stationData.value.longitude) {
    uni.showToast({ title: '导航信息不完整', icon: 'none' });
    return;
  }
  
  // 使用腾讯地图导航
  uni.openLocation({
    latitude: stationData.value.latitude,
    longitude: stationData.value.longitude,
    name: stationData.value.name,
    address: stationData.value.address,
    scale: 18
  });
};
</script>

<style scoped>
.container {
  background-color: #f8f8f8;
  min-height: 100vh;
}

.nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.nav-back {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  width: 20px;
  height: 20px;
}

.nav-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.nav-right {
  width: 40px;
}

.station-image-container {
  margin-top: 44px;
}

.station-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.station-info {
  background-color: #fff;
  padding: 16px;
  margin-bottom: 10px;
}

.station-name {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
}

.station-meta {
  margin-bottom: 16px;
}

.meta-item {
  display: flex;
  margin-bottom: 8px;
  font-size: 14px;
}

.meta-label {
  color: #666;
  margin-right: 4px;
}

.meta-value {
  color: #333;
  flex: 1;
}

.action-buttons {
  display: flex;
  justify-content: space-around;
  border-top: 1px solid #eee;
  padding-top: 16px;
}

.call-btn,
.navigate-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}

.btn-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 4px;
}

.btn-text {
  font-size: 12px;
  color: #666;
}

.oil-price-section {
  background-color: #fff;
  padding: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}

.oil-price-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.oil-price-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
}

.oil-price-item:last-child {
  border-bottom: none;
}

.oil-type {
  font-size: 16px;
  color: #333;
}

.oil-price {
  font-size: 18px;
  font-weight: 600;
  color: #ff6b35;
}
</style>